# date:20210328
from typing import List
import collections

# 时间： 13%， 内存：98%
class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        result = []
        tmp = []
        intervals.sort()
        for interval in intervals:
            if tmp:
                if not ((tmp[0] > interval[1]) or (interval[0] > tmp[1])):
                    tmp = [min(tmp[0], interval[0]), max(tmp[1], interval[1])]
                else:
                    result.append(tmp)
                    tmp = interval
            else:
                tmp = interval
        result.append(tmp)
        return result

intervals = [[1,3],[2,6],[8,10],[15,18]]
model = Solution().merge(intervals)
print(model)



intervals = [[1,4],[0,4]]
model = Solution().merge(intervals)
print(model)


intervals = [[1,4],[2,3]]
model = Solution().merge(intervals)
print(model)